package com.venusj.dao;

import com.venusj.ipersistence.io.Resources;
import com.venusj.ipersistence.sqlSession.SqlSession;
import com.venusj.ipersistence.sqlSession.SqlSessionFactory;
import com.venusj.ipersistence.sqlSession.SqlSessionFactoryBuilder;
import com.venusj.pojo.User;
import org.dom4j.DocumentException;

import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.util.List;

/**
 * @author zhangjh
 * @date 2020/10/16
 * @desc
 */
public class UserDaoImpl implements UserDao{


    @Override
    public List<User> findAll() throws PropertyVetoException, DocumentException {

        InputStream inputStream = Resources.getResourceAsStream("SqlMapperConfig.xml");

        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = build.openSession();
        User user = new User();
        user.setId(1);
//        user.setUsername("张山");
//        User user2 = sqlSession.selectOne("user.selectOne", user);
//        System.out.println(user2.toString());
        List<User> userList = sqlSession.selectList("user.selectList");
        userList.forEach(u -> {
            System.out.println(u.toString());
        });

        return userList;
    }

    @Override
    public User findByCondition(User user) throws PropertyVetoException, DocumentException {

        InputStream inputStream = Resources.getResourceAsStream("SqlMapperConfig.xml");

        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = build.openSession();
        User user2 = sqlSession.selectOne("user.selectOne", user);
        System.out.println(user2.toString());

        return user2;
    }
}
